package wang.unclecat.datacenter;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.utils.net.Uri;
import wang.unclecat.datacenter.db.Alarm;
import wang.unclecat.datacenter.db.AlarmsDBOperation;
import wang.unclecat.mylibrary.MyLog;
import wang.unclecat.mylibrary.multidevicekit.SelectDeviceDialog;

import java.util.List;

import static ohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

/**
 * 测试数据库crud
 */
public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        requestPermissionsFromUser(new String[]{"wang.unclecat.datacenter.DataAbilityShellProvider.PROVIDER", "ohos.permission.DISTRIBUTED_DATASYNC", "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO", "ohos.permission.GRT_BUNDLE_INFO", "ohos.permission.servicebus.ACCESS_SERVICE", "com.huawei.hwddmp.servicebus.BIND_SERVICE"}, 0);
//
//        if (verifySelfPermission(DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
//            // 没有权限
//            if (canRequestPermission(DISTRIBUTED_DATASYNC)) {
//                // 弹框
//                requestPermissionsFromUser(
//                        new String[]{DISTRIBUTED_DATASYNC,  "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"}, 0);
//            }
//        }

        super.setUIContent(ResourceTable.Layout_ability_main);
        findComponentById(ResourceTable.Id_insert).setClickedListener(component -> {
            AlarmsDBOperation.insert(this);
        });

        findComponentById(ResourceTable.Id_query).setClickedListener(component -> {
            Alarm alarm = AlarmsDBOperation.queryFirst(this);
            if (alarm!=null) {
                MyLog.info(alarm.toString());

            }
        });

        findComponentById(ResourceTable.Id_queryAll).setClickedListener(component -> {
            List<Alarm> list = AlarmsDBOperation.queryAll(this);
            if (list!=null) {
                for (Alarm alarm : list) {
                    MyLog.info(alarm.toString());
                }
            }
        });

        findComponentById(ResourceTable.Id_queryAllRemote).setClickedListener(component -> {

            new SelectDeviceDialog(getContext(), deviceInfo -> {
                String uriStr = String.format("dataability://%s/wang.unclecat.datacenter.remote.AlarmsDataAbility", deviceInfo.getDeviceId());
                MyLog.info("uriStr : "+uriStr);
                Uri uri = Uri.parse(uriStr);
//                Uri uri = Uri.parse("dataability:///wang.unclecat.datacenter.remote.AlarmsDataAbility");
//                uri = uri.makeBuilder().decodedAuthority(deviceInfo.getDeviceId()).build();

                List<Alarm> list = AlarmsDBOperation.queryAllRemote(this, uri);
                if (list!=null) {
                    for (Alarm alarm : list) {
                        MyLog.info(alarm.toString());
                    }
                }

            }).show();

        });
    }
}
